package cn.hls.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import cn.hls.dao.OrgDao;
import cn.hls.entity.Org;
import cn.hls.entity.vo.ExtTree;
import cn.hls.entity.vo.PageInfo;
import cn.hls.service.OrgService;

/**
 * @fileName: MoudleService.java
 * @Description: 组织机构Service接口实现类
 * @author: yinghui.zhang
 * @email: uuhui@163.com
 * @date 2015年4月15日
 * @version V1.0
 */
@Service("orgService")
public class OrgServiceBean extends BaseServiceBean<Org> implements OrgService {
	@Resource(name = "orgDao")
	private OrgDao orgDao;

	@Override
	public boolean add(Org entity) throws Exception {
		if (entity.getParent() != null) {
			// 设置上级的模块对象
			entity.setParent(orgDao.find(entity.getParent().getId()));
		}
		return super.add(entity);
	}
	
	@Override
	public boolean update(Org entity) throws Exception {
		Org tempEntity=orgDao.find(entity.getId());
		BeanUtils.copyProperties(entity, tempEntity);
		tempEntity.setUpdateTime(new Date());
		return super.update(tempEntity);
	}

	@Override
	public List<ExtTree> loadChildOrgInfo(Org org, Object nodeId)
			throws Exception {
		return orgDao.loadChildOrgInfo(org, nodeId);
	}

	@Override
	public PageInfo<Org> loadOrgInfo(Org entity, PageInfo<Org> page)
			throws Exception {
		return orgDao.loadOrgInfo(entity, page);
	}

	@Override
	public String deleteOrg(Long id) throws Exception {
		return orgDao.deleteOrg(id);
	}

	@Override
	public void updateOrgSort(String ids) throws Exception {
		orgDao.updateOrgSort(ids);

	}

	@Override
	public Org findOrg(Org entity) throws Exception {
		return orgDao.findOrg(entity.getName().trim(), entity.getCode().trim(),entity.getParent()!=null?entity.getParent().getId():null);
	}

	@Override
	public Map<String, Long> loadOrgInfo() throws Exception {
		return orgDao.loadOrgInfo();
	}

}
